﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        RoleAssign.aspx.cs       
/// Author :         Wangbc
/// CDT:             2011-04-01           
/// Version:         1.0.0.0                
/// Depiction:       分配权限    
/// </summary>
public partial class main_admin_RoleAssign : System.Web.UI.Page
{
    #region 常量
    /// <summary>
    /// 权限常量
    /// </summary>
    private const string FUNCTIONS = "functions";
    #endregion 

    #region 变量
    /// <summary>
    /// 菜单信息
    /// </summary>
    private ArrayList menus = new ArrayList();
    /// <summary>
    /// 权限
    /// </summary>
    private string functions = string.Empty;
     #endregion 

    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //先前有的权限
        functions = Request.QueryString[FUNCTIONS]; 
        //取得权限XMLNode
        XmlNode AllNode = ConfigAndElse.SystemFasade.getAllMenuXmlNode();
        //菜单
        getMenus(AllNode, menus);
        //设置
        setPageMenus(menus);
    }

    /// <summary>
    /// 设置菜单数据
    /// </summary>
    /// <param name="menus"></param>
    public void setPageMenus(ArrayList menus)
    {
        if (menus != null)
        {
            StringBuilder strBldr = new StringBuilder();
            strBldr.AppendFormat("<script language=\"javascript\">", Environment.NewLine);
            strBldr.AppendFormat("var tree_ar = new Array(");
            //级别，名字，ID，可否选中
            for (int i = 0; i < menus.Count; i++)
            {
                string [] strFunc = ((string)menus[i]).Split(",".ToCharArray());
                strBldr.AppendFormat(" new Array({0},'{1}','{2}','{3}'),",strFunc[0],strFunc[1],strFunc[2],strFunc[3]);
            }

            strBldr.AppendFormat("new Array(1,'','',0));{0}", Environment.NewLine);
            strBldr.AppendFormat("</script>{0}", Environment.NewLine);

            Page.ClientScript.RegisterStartupScript(Page.GetType(), "confirm", strBldr.ToString());
        }
            
    
    }

    /// <summary>
    /// 是否有权限
    /// </summary>
    /// <returns></returns>
    private bool isPriv(string id) {
        bool result = false;
        if (functions != null) {
            string[] func = functions.Split(",".ToCharArray());
            for (int i = 0; i < func.Length; i++)
            {
                if (id.Equals(func[i]))
                {
                    return true;
                }
                else {

                    string temp = division(func[i]);
                    string[] temparr = temp.Split(",".ToCharArray());
                    for (int k = 0; k < temparr.Length; k++)
                    {
                        if (temparr[k].Equals(id))
                        {
                            return true;
                        }
                    }                
                }
            }
              
        }
        return result;
    }

    /// <summary>
    /// 拆分字符串
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    private string division(string s)
    {
        string b = "";
        string c = "";
        for (int i = 0; i < s.Length; i++)
        {
            b = b + s[i];
            c = c + b + ",";
        }
        return c;
    }

    /// <summary>
    /// 取得XML所有菜单
    /// </summary>
    /// <returns></returns>
    public void getMenus(XmlNode AllNode,ArrayList menu)
    {      
        XmlNodeList nodeList = AllNode.SelectNodes("Menu");//获取bookstore节点的所有子节点
        foreach (XmlNode xn in nodeList)//遍历所有子节点
        {
            XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型        
            string id = xe.GetAttribute("id");
            string priv = isPriv(id) == true ? "1" : "0";
            string m = xe.GetAttribute("level") + "," + xe.GetAttribute("name") + "," + id + "," + priv;
            menu.Add(m);
            getMenus(xn, menu);
        }
    
    }
}
